package com.changgou.seckill.controller;

import com.changgou.common.constant.StatusCode;
import com.changgou.common.util.TokenDecode;
import com.changgou.common.vo.Result;
import com.changgou.seckill.pojo.SeckillOrder;
import com.changgou.seckill.service.SeckillOrderService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/****
 * @Author:www.itheima.com
 * @Description:
 * @Date www.itheima.com
 *****/

@RestController
@RequestMapping("/seckill/order")
@CrossOrigin
public class SeckillOrderController {

    @Autowired
    private SeckillOrderService seckillOrderService;


    /**
    * 生成当前用户的秒杀订单
    * @param time 秒杀商品对应的秒杀时间段
    * @param id 秒杀商品id
    * @return 秒杀结果
    */
    @GetMapping("/create")
    public Result create(String time, Long id){
        try {
            String username= TokenDecode.getUserInfo().get("username");
            //String username = SecurityContextHolder.getContext().getAuthentication().getName();
            Long SeckKillOrderId = seckillOrderService.create(username,time,id);
            if (SeckKillOrderId!=null){
                return new Result("秒杀商品成功！", SeckKillOrderId);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, e.getMessage());
        }
        return new Result(false, StatusCode.ERROR, "秒杀商品失败！");
    }

    /***
     * SeckillOrder分页条件搜索实现
     * @param seckillOrder: 查询条件
     * @param page:当前页
     * @param size:每页显示多少条
     * @return 查询结果
     */
    @PostMapping(value = "/search/{page}/{size}" )
    public Result<PageInfo<SeckillOrder>> findPage(@RequestBody(required = false)  SeckillOrder seckillOrder, @PathVariable  int page, @PathVariable  int size){
        //调用SeckillOrderService实现分页条件查询SeckillOrder
        PageInfo<SeckillOrder> pageInfo = seckillOrderService.findPage(seckillOrder, page, size);
        return new Result<PageInfo<SeckillOrder>>("查询成功！", pageInfo);
    }

    /***
     * SeckillOrder分页搜索实现
     * @param page:当前页
     * @param size:每页显示多少条
     * @return 查询结果
     */
    @GetMapping(value = "/search/{page}/{size}" )
    public Result<PageInfo<SeckillOrder>> findPage(@PathVariable  int page, @PathVariable  int size){
        //调用SeckillOrderService实现分页查询SeckillOrder
        PageInfo<SeckillOrder> pageInfo = seckillOrderService.findPage(page, size);
        return new Result<PageInfo<SeckillOrder>>("查询成功！", pageInfo);
    }

    /***
     * 多条件搜索品牌数据
     * @param seckillOrder 查询条件
     * @return 查询结果
     */
    @PostMapping(value = "/search" )
    public Result<List<SeckillOrder>> findList(@RequestBody(required = false)  SeckillOrder seckillOrder){
        //调用SeckillOrderService实现条件查询SeckillOrder
        List<SeckillOrder> list = seckillOrderService.findList(seckillOrder);
        return new Result<List<SeckillOrder>>("查询成功！", list);
    }

    /***
     * 根据ID删除品牌数据
     * @param id 主键
     * @return 查询结果
     */
    @DeleteMapping(value = "/{id}" )
    public Result delete(@PathVariable Long id){
        //调用SeckillOrderService实现根据主键删除
        seckillOrderService.delete(id);
        return new Result(true, StatusCode.OK, "删除成功！");
    }

    /***
     * 修改SeckillOrder数据
     * @param seckillOrder 更新对象
     * @return 操作结果
     */
    @PutMapping
    public Result update(@RequestBody  SeckillOrder seckillOrder){
        //调用SeckillOrderService实现修改SeckillOrder
        seckillOrderService.update(seckillOrder);
        return new Result(true, StatusCode.OK, "修改成功！");
    }

    /***
     * 新增SeckillOrder数据
     * @param seckillOrder 新增对象
     * @return 操作结果
     */
    @PostMapping
    public Result add(@RequestBody  SeckillOrder seckillOrder){
        //调用SeckillOrderService实现添加SeckillOrder
        seckillOrderService.add(seckillOrder);
        return new Result(true, StatusCode.OK, "添加成功！");
    }

    /***
     * 根据ID查询SeckillOrder数据
     * @param id 主键
     * @return 查询结果
     */
    @GetMapping("/{id}")
    public Result<SeckillOrder> findById(@PathVariable Long id){
        //调用SeckillOrderService实现根据主键查询SeckillOrder
        SeckillOrder seckillOrder = seckillOrderService.findById(id);
        return new Result<SeckillOrder>("查询成功！", seckillOrder);
    }

    /***
     * 查询SeckillOrder全部数据
     * @return 查询结果
     */
    @GetMapping
    public Result<List<SeckillOrder>> findAll(){
        //调用SeckillOrderService实现查询所有SeckillOrder
        List<SeckillOrder> list = seckillOrderService.findAll();
        return new Result<List<SeckillOrder>>("查询成功！", list);
    }
}
